import random

class Room:
    def __init__(self, room_id):
        self.room_id = room_id
        self.players = [None, None]  # 房间里面的人，初始化为空


    def exit_room(self, player_id):
        if player_id in self.players:  # 如果玩家在房间里面
            index = self.players.index(player_id)  # 找到玩家所在的位置
            self.players[index] = None  # 将该位置清空
            return True
        else:
            return False

class Player:
    def __init__(self, player_id, gold=500):
        self.player_id = player_id
        self.gold = gold
        self.room_id = None  # 初始化玩家不在房间里面

    def join_room(self, room_id):
        if self.room_id is None:  # 如果玩家不在房间里面
            room = rooms.get(room_id)
            if room is not None and room.enter_room(self.player_id):  # 如果房间存在且加入成功
                self.room_id = room_id  # 将玩家加入房间
                return True
        return False



# 创建房间
rooms = {}
for i in range(1, 6):
    rooms[i] = Room(i)

# 创建玩家
players = {}
for i in range(1, 11):
    players[i] = Player(i)

while True:

    choice = input("1. 创建房间\n2. 加入房间\n3. 离开房间\n4. 开始游戏\n请选择你的操作(按q退出)：")

    if choice == "1":
        room_id = int(input("请输入房间号："))
        if rooms.get(room_id) is None:  # 如果房间不存在
            rooms[room_id] = Room(room_id)  # 创建新房间
            print("创建房间成功！")
        else:
            print("房间已存在！")

    elif choice == "2":
        player_id = int(input("请输入玩家号："))
        room_id = int(input("请输入房间号："))
        if players.get(player_id) is None:  # 如果玩家不存在
            print("玩家不存在！")
        elif rooms.get(room_id) is None:  # 如果房间不存在
            print("房间不存在！")
        elif players[player_id].join_room(room_id):  # 如果加入房间成功
            print("加入房间成功！")
        else:
            print("加入房间失败！")

    elif choice == "3":
        pass
    elif choice == "4":
       pass


    elif choice == "q":
        break

    else:
        print("请输入正确的操作！")
